#include "StdAfx.h"
#include "tools.h"




BOOL tools::create_sub_folders( std::string szFilePath )
{
	if (szFilePath.empty())
		return FALSE;

	std::size_t pos_slash = szFilePath.rfind(_T('\\'), szFilePath.length() - 1);

	szFilePath.erase(pos_slash + 1);

	DWORD attributes = GetFileAttributes(szFilePath.c_str());	
	if (0xFFFFFFFF ==  attributes)
	{
		INT res = SHCreateDirectoryEx(NULL, szFilePath.c_str(), NULL);
		if (ERROR_SUCCESS != res)
		{
			return FALSE;
		}
	}

	return TRUE;
}

BOOL tools::split_path( IN const std::string& path, OUT std::string* p_drive /*= NULL*/, OUT std::string* p_dir /*= NULL*/, OUT std::string* p_name /*= NULL*/, OUT std::string* p_ext /*= NULL*/, OUT std::string* p_name_and_ext /*= NULL*/ )
{
	char drive[ _MAX_DRIVE ];
	char dir[ _MAX_DIR ];
	char fname[ _MAX_FNAME ];
	char ext[ _MAX_EXT ];

	std::string temp = "";

	if (NULL != _splitpath_s( path.c_str(), drive, dir, fname, ext))
		return FALSE;

	if (p_drive)
		*p_drive = drive;

	if (p_dir)
		*p_dir = dir;

	if (p_name)
		*p_name = fname;

	if (p_ext)
		*p_ext = ext;

	if (p_name_and_ext)
	{
		temp = fname;
		temp += ext;
		*p_name_and_ext = temp;
	}
	return TRUE;
}

BOOL tools::get_module_directory( OUT std::string& path )
{
	char m_File[ _MAX_PATH ]; // file name
	std::string drive;
	std::string dir;

	if(FALSE == GetModuleFileName( NULL, m_File, _countof( m_File ) - 1 ) )
	{
		return FALSE;
	}
	else
	{
		split_path(m_File, &drive, &dir);
	}

	path = drive.c_str();
	path += dir.c_str();

	return TRUE;
}
